package com.example.springboot.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.base.Result;
import com.example.springboot.mapper.FileInfoMapper;
import com.example.springboot.mapper.FolderFileInfoMapper;
import com.example.springboot.mapper.FolderInfoMapper;
import com.example.springboot.pojo.FileInfo;
import com.example.springboot.pojo.FolderFileInfo;
import com.example.springboot.pojo.FolderInfo;
import com.example.springboot.service.FolderFileInfoService;
import com.example.springboot.utils.ResultMap;
import com.example.springboot.utils.Constants;
import com.example.springboot.utils.HdfsUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class FolderFileInfoServiceImpl extends ServiceImpl<FolderFileInfoMapper, FolderFileInfo> implements FolderFileInfoService {

    @Resource
    FolderFileInfoMapper folderFileInfoMapper;

    @Resource
    FolderInfoMapper folderInfoMapper;

    @Resource
    FileInfoMapper fileInfoMapper;


    @Override
    public Result getFolderContent(String folderId) {
        // 查询folderid下的所有文件和文件夹
        // 先查询文件夹
        List<Map<String, Object>> folderInfoMap = folderInfoMapper.selectJoinMaps(new MPJLambdaWrapper<>()
                .select(FolderInfo::getFolderName, FolderInfo::getFolderCreateTime, FolderInfo::getFolderDel)
                .eq(FolderInfo::getFolderDel, 1) //文件未删除
                .select(FolderFileInfo::getFolderFileId)
                .leftJoin(FolderFileInfo.class, FolderFileInfo::getFolderFileId, FolderInfo::getFolderId)
                .eq(FolderFileInfo::getFolderPd, folderId)
                .eq(FolderFileInfo::getFolderFileType, Constants.FILE.FOLDER.getValue()));
        // 再查询文件
        List<Map<String, Object>> fileInfoMap = fileInfoMapper.selectJoinMaps(new MPJLambdaWrapper<>()
                .select(FileInfo::getFileName, FileInfo::getFileUploadTime)
                .select(FolderFileInfo::getFolderFileId)
                .leftJoin(FolderFileInfo.class, FolderFileInfo::getFolderFileId, FileInfo::getFileId) // 左连接
                .eq(FolderFileInfo::getFolderPd, folderId)
                .eq(FolderFileInfo::getFolderFileType, Constants.FILE.FILE.getValue())
                .eq(FileInfo::getFileDel, Constants.FILE.NO_DEL.getValue())); //文件未删除
        return Result.success(new ResultMap().put("folder", folderInfoMap).put("file", fileInfoMap));

    }

    @Override
    public Result createFolder(String folderId, String folderName) {
        FolderInfo folderInfo = folderInfoMapper.selectOne(new MPJLambdaWrapper<FolderInfo>()
                .select(FolderInfo::getFolderUrl, FolderInfo::getUserId)
                .eq(FolderInfo::getFolderId, folderId));
        String folderPath = folderInfo.getFolderUrl() + "/" + folderName;
        // 创建文件夹
        HdfsUtil.createFolder(folderPath);

        // 插入文件夹信息
        FolderInfo folder = new FolderInfo();
        String newFolderId = IdUtil.simpleUUID();
        String userId = folderInfo.getUserId();
        folder.setFolderId(newFolderId);
        folder.setFolderUrl(folderPath);
        folder.setUserId(userId);
        folder.setFolderName(folderName);
        folder.setFolderDel(1);
        folderInfoMapper.insert(folder);

        // 插入文件夹所属
        FolderFileInfo folderFileInfo = new FolderFileInfo();
        String folderFileInfoId = IdUtil.simpleUUID();
        folderFileInfo.setFolderFileInfoId(folderFileInfoId);
        folderFileInfo.setFolderFileId(newFolderId);
        folderFileInfo.setFolderFileType(1);
        folderFileInfo.setFolderPd(folderId);
        folderFileInfoMapper.insert(folderFileInfo);
        return Result.success("创建成功");
    }

    @Override
    public Result deleteFolder(String folderId) {
        FolderInfo folderInfo = new FolderInfo();
        folderInfo.setFolderId(folderId);
        folderInfo.setFolderDel(Constants.FILE.IS_DEL.getValue());
        folderInfoMapper.updateById(folderInfo);
        return Result.success("删除成功");
    }
}
